# build stage
FROM maven:3.9.-eclipse-temurin-17 AS build
WORKDIR /app
COPY pom.xml ./
COPY .mvn .mvn
COPY src ./src
RUN mvn -B -DskipTests package

# runtime stage
FROM eclipse-temurin:17-jre-jammy
WORKDIR /opt/app
ARG JAR_FILE=target/*.jar
COPY --from=build /app/${JAR_FILE} app.jar
RUN addgroup --system app && adduser --system --ingroup app app
USER app
EXPOSE 8080
ENTRYPOINT [ "bash", "-lc", "echo Waiting for DB...; while ! bash -c 'cat < /dev/tcp/${DB_HOST:-db}/${DB_PORT:-3306}' 2>/dev/null; do sleep 1; done; echo DB ready; java -jar /opt/app/app.jar" ]
